<?php
require_once '../dbms_university_courses_mvc/Connection.php';
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';

//get available periods for this classroom 
$periods = Period::getPeriods ();
$days = Day::getDays ();

/* @var $course Course */
$course = Course::constructFrom_course_id ( $_GET ['course_id'] );

$coursePeriods = CoursePeriod::getCoursePeriods_by_course_id ( $_GET ['course_id'] );

//get the course periods that are in the same classroom, but a different course
$sql = "SELECT course_periods.* FROM course_periods LEFT JOIN courses ON course_periods.course_id = courses.course_id WHERE course_periods.course_id != " . $course->get_course_id () . " AND course_periods.classroom_id = " . $course->get_classroom_id () . " AND courses.term_id = " . $course->get_term_id();
$result_other_courses_periods = mysql_query ( $sql );

$other_courses_periods = array ();
while ( $other_courses_periods_row = mysql_fetch_array ( $result_other_courses_periods ) ) {
	array_push ( $other_courses_periods, CoursePeriod::constructFrom_course_periods_row ( $other_courses_periods_row ) );
}

/* @var $instructor Instructor */
$instructors = Instructor::getInstructors_by_profile_id ( $_SESSION ['profile_id'] );
$instructor = $instructors [0];

$sql = "SELECT course_periods.* FROM courses LEFT JOIN course_periods ON courses.course_id = course_periods.course_id WHERE courses.course_id != " . $course->get_course_id () . " AND courses.instructor_id=" . $instructor->get_instructor_id () . " AND courses.term_id = " . $course->get_term_id();
$result_other_courses_periods = mysql_query ( $sql );
$instructor_other_course_periods = array ();
while ( $other_courses_periods_row = mysql_fetch_array ( $result_other_courses_periods ) ) {
	array_push ( $instructor_other_course_periods, CoursePeriod::constructFrom_course_periods_row ( $other_courses_periods_row ) );
}

?>

<h1>Select Periods for <?php
echo $course->get_course_name ();
?></h1>
<?php
/* @var $day Day */
/* @var $period Period */
//iterate the days
foreach ( $days as $day ) {
	?>
<br>
<br>
<h2><?php
	echo $day->get_day ();
	?></h2>
&nbsp;&nbsp;&nbsp;&nbsp;<?php
	foreach ( $periods as $period ) {
		$other_course_selected_period = periodSelected ( $period->get_period_id (), $day->get_day_id (), $other_courses_periods );
		$instructor_other_course_selected_period = periodSelected ( $period->get_period_id (), $day->get_day_id (), $instructor_other_course_periods );
		
		?><span <?php
		if ($other_course_selected_period) {
			?>
	style="color: gray;"
	title="This classroom/period is already occupied by another course"
	<?php
		}
		else if ($instructor_other_course_selected_period) {
			?>
	style="color: gray;"
	title="You are already teaching a course this period this term"
	<?php
		}
		?>
>
		<?php
		echo $period->get_period_id ()?>
		</span>
<script type="text/javascript">
		<?php
		
		$coursePeriod = periodSelected ( $period->get_period_id (), $day->get_day_id (), $coursePeriods );
		if ($coursePeriod) {
			?>
				var course_period_id<?php
			echo $period->get_period_id ();
			?><?php

			echo $day->get_day_id ();
			?> = <?php
			$coursePeriod?>;
				<?php
		} else {
			?>
		var course_period_id<?php
			echo $period->get_period_id ();
			?><?php

			echo $day->get_day_id ();
			?>;
		<?php
		}
		?>
	function periodSelected<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?>(id){
		course_period_id<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?> = id;
	}
</script>
<input type="checkbox"
	onchange="
	//INSERT on check, DELECT on uncheck
	if(this.checked){
		insertNewCoursePeriod(<?php
		echo $course->get_course_id ();
		?>, <?php
		echo $period->get_period_id ();
		?>, <?php
		echo $day->get_day_id ();
		?>, <?php
		echo $course->get_classroom_id ();
		?>, periodSelected<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?>);
	}
	else{
		var prop = new Object;
		prop.course_period_id = course_period_id<?php
		echo $period->get_period_id ();
		?><?php

		echo $day->get_day_id ();
		?>;
			var coursePeriod = new CoursePeriod(prop);
			coursePeriod.deleteMeFromDatabase();
	}
"
	<?php
		if ($coursePeriod) {
			?> checked="checked"
	<?php
		} else if ($other_course_selected_period || $instructor_other_course_selected_period) {
			?>
	disabled="disabled"
	<?php
		}
		?>
/>
<?php
	}
	?>
<?php
}

function periodSelected($period_id, $day_id, $coursePeriods) {
	/* @var $coursePeriod CoursePeriod */
	foreach ( $coursePeriods as $coursePeriod ) {
		if ($coursePeriod->get_period_id () == $period_id && $coursePeriod->get_day_id () == $day_id) {
			return $coursePeriod->get_course_period_id ();
		}
	}
	return false;
}

?>
<br>
<br>
<h2>Period Reference</h2>
<table>
<?php
foreach ( $periods as $period ) {
	?><tr>
		<td>
	<?php
	echo $period->get_period_id ();
	?>
	</td>
		<td>
	<?php
	echo $period->get_start_time ();
	?>
	</td>
		<td>
	<?php
	echo $period->get_end_time ();
	?>
	</td>
	</tr>
	<?php
}
?>
</table>

